﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebUI.Models.Tables;

namespace WebUI.Common
{
    public class Helper
    {
        /// <summary>
        /// 将分类LIST排序
        /// </summary>
        /// <typeparam name="T">类型,继承自CategoryModel</typeparam>
        /// <param name="list">原数据</param>
        /// <param name="parentID">父ID,默认0</param>
        /// <param name="level">等级,默认0</param>
        /// <returns></returns>
        public static List<T> GetCategoryNoLevel<T>(List<T> list, int parentID = 0, int level = 0) where T : CategoryModel
        {
            List<T> result = new List<T>();
            result = list.Where(m => m.ParentID == parentID).ToList();
            for (int i = 0; i < result.Count; i++)
            {
                for (int j = 0; j < level; j++)
                {
                    result[i].CatName = "-" + result[i].CatName;
                }
                var subResult = GetCategoryNoLevel<T>(list, result[i].ID, level + 1);
                if (subResult.Count != 0)
                {
                    result.InsertRange(i + 1, subResult);
                    i += subResult.Count;
                }
            }
            return result;
        }

        /// <summary>
        /// 将导航排序
        /// </summary>
        /// <param name="list">原数据</param>
        /// <param name="parentID">父ID,默认0</param>
        /// <param name="level">等级,默认0</param>
        /// <returns></returns>
        public static List<Nav> GetNavNoLevel(List<Nav> list, int parentID = 0, int level = 0)
        {
            List<Nav> result = new List<Nav>();
            result = list.Where(m => m.ParentID == parentID).ToList();
            for (int i = 0; i < result.Count; i++)
            {
                for (int j = 0; j < level; j++)
                {
                    result[i].NavName = "-" + result[i].NavName;
                }
                var subResult = GetNavNoLevel(list, result[i].ID, level + 1);
                if (subResult.Count != 0)
                {
                    result.InsertRange(i + 1, subResult);
                    i += subResult.Count;
                }
            }
            return result;
        }

        /// <summary>
        /// 将单页面排序
        /// </summary>
        /// <param name="list">原数据</param>
        /// <param name="parentID">父ID,默认0</param>
        /// <param name="level">等级,默认0</param>
        /// <returns></returns>
        public static List<SinglePage> GetSinglePageNoLevel(List<SinglePage> list, int parentID = 0, int level = 0)
        {
            List<SinglePage> result = new List<SinglePage>();
            result = list.Where(m => m.ParentID == parentID).ToList();
            for (int i = 0; i < result.Count; i++)
            {
                for (int j = 0; j < level; j++)
                {
                    result[i].PageName = "-" + result[i].PageName;
                }
                var subResult = GetSinglePageNoLevel(list, result[i].ID, level + 1);
                if (subResult.Count != 0)
                {
                    result.InsertRange(i + 1, subResult);
                    i += subResult.Count;
                }
            }
            return result;
        }
    }
}